﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace g2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string str= textBox1.Text;
            string buf = @"[0-9]{6}(18|19|20)[0-9]{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)[0-9]{3}[0-9xX]";
            Regex r = new Regex(buf);
           // Regex r = new Regex(@"/^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/");
            bool b = r.IsMatch(str);
            bool b2 = false;
            int sum = 0;
            int[] prefix = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
            char[] chars = new char[] { '1','0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
            for (int i = 0; i < prefix.Length; i++)
            {
                sum = sum + prefix[i] * (str[i] - '0');
            }
            sum = sum % 11;
            if(str[17]=='x'||str[17]=='X')
            {
                if(chars[sum] == 'X')
                {
                    b2 = true;
                }
            }
            else if (chars[sum] == str[17])
            {
                b2=true;
            }
            if (b&&b2)
            {
                label2.Text = "合法";
            }else
            {
                label2.Text = "不合法";
            }
        }
    }
}
